
(function(){
'use strict';

angular.module('Contacts.users')

        .controller('UsersController', function ($stateParams, $http, $rootScope, $scope, ngDialog) {

            var users = this;
            $rootScope.maxSize = 12;
            
            $http({
                url: 'user/letters',
                method: "GET"
            }).success(function (data) {
                $rootScope.letters = data;
            });
            
            if (!$rootScope.currentPage){
	            $http({
	                url: 'user/',
	                method: "GET",
	                params: {
	                    "page": 1
	                }
	            }).success(function (data) {
	            	$rootScope.page = data;
	                $rootScope.numPerPage = data.size;
	                $rootScope.currentPage = 1;
	                $rootScope.totalElements = data.totalElements;
	                $rootScope.totalPages = data.totalPages;
	                $rootScope.numberOfElements = data.numberOfElements;
	            });
            }
            
            
            this.getLetters = function () {

                $http({
                    url: 'user/letters',
                    method: "GET"
                }).success(function (data) {
                    $rootScope.letters = data;
                });
            };
            
            
            
            this.getUsers = function (page, letter) {

                if (page == null || page < 1)
                    page = 1;
                if (letter){
	                $http.post('user/letter',{
	                	"letter": letter,
	                     "page": page,
	                }).success(function (data) {
	                    $rootScope.page = data;
	                    $rootScope.numPerPage = data.size;
	                    $rootScope.currentPage = page;
	                    $rootScope.letter = letter;
	                    $rootScope.totalElements = data.totalElements;
	                    $rootScope.totalPages = data.totalPages;
	                    $rootScope.numberOfElements = data.numberOfElements;
	                });
                }
                else{
                	$http({
                        url: 'user/',
                        method: "GET",
                        params: {
                            "page": page
                        }
                    }).success(function (data) {
                        $rootScope.page = data;
                        $rootScope.numPerPage = data.size;
                        $rootScope.currentPage = page;
                        $rootScope.letter = false;
                        $rootScope.totalElements = data.totalElements;
                        $rootScope.totalPages = data.totalPages;
                        $rootScope.numberOfElements = data.numberOfElements;
                    });
                }
            };
            
            this.makeFullName = function (firstName, middleName, lastName) {
                $rootScope.fullName = '' + firstName +' '+ (middleName ? middleName : "") +' '+ lastName;
            };
            
            
            
            $scope.numPages = function () {
                return Math.ceil($rootScope.page.content.length / $scope.numPerPage);
            };
            
            this.openDeleteUserDialog = function (userId) {
            	
            	$scope.userId = userId;
            	
                ngDialog.close();
                ngDialog.open({
					template: 'app/users/dialogDeleteUser.html',
					scope: $scope
                });
            };
            
            this.openEditUserDialog = function (user) {
            	
            	$scope.userToModify = angular.copy(user);
            	
//                ngDialog.close();
                ngDialog.open({
					template: 'app/users/dialogEditUser.html',
					scope: $scope
                });
            };
            
            this.removeUser = function (id) {
                $http({
                    url: 'user/delete/' + id,
                    method: "DELETE"
                }).success(function (data) {
                	if ($rootScope.numberOfElements <= 1){
                		users.getUsers($rootScope.currentPage-1, $rootScope.letter);
                    }
                    else{
                    	users.getUsers($rootScope.currentPage, $rootScope.letter);
                    }
                	users.getLetters();
                	if ($rootScope.currentUser == id){
                		$rootScope.hideInfo = true;
                	}
                });
                ngDialog.close();
            };
            
            this.createUser = function (firstName, middleName, lastName) {
            	if (middleName){
            		middleName = middleName.trim();
            	}
            	else{
            		middleName="";
            	}
                $http.put('user/create', {
                    "firstName": firstName.trim(),
                    "middleName": middleName.trim(),
                    "lastName": lastName.trim(),
                }).success(function (data) {
                    users.getUsers($rootScope.currentPage, $rootScope.letter);
                    users.getLetters();
                }).error(function (data, status) {
                	$scope.errorMessage = data.message;
            		ngDialog.close();
                    ngDialog.open({
                        template: 'app/users/dialogCreateFail.html',
                        scope: $scope
                    });
                });
                ngDialog.closeAll();
            };
            
            this.openCreateUserDialog = function () {
                ngDialog.close();
                ngDialog.open({
                    template: 'app/users/dialogCreateUser.html'
                });
            };
            
            this.updateUser = function (id, firstName, middleName, lastName) {
            	if (middleName){
            		middleName = middleName.trim();
            	}
            	else{
            		middleName="";
            	}
                $http.post('user/update', {
                	"id": id,
                    "firstName": firstName.trim(),
                    "middleName": middleName,
                    "lastName": lastName.trim()
                }).success(function (data) {
                    users.getUsers($rootScope.currentPage, $rootScope.letter);
                    users.getLetters();
                }).error(function (data, status) {
                	$scope.errorMessage = data.message;
            		ngDialog.close();
                    ngDialog.open({
                        template: 'app/users/dialogCreateFail.html',
                        scope: $scope
                    });
                });
//                ngDialog.closeAll();
            };
            
            this.closeAll = function () {
                ngDialog.closeAll();
            };

            $scope.$watch("currentPage", function (newValue, oldValue) {
            	if (newValue !== oldValue) {
            		if ($scope.letter){
            			$http({
    	                    url: 'user/'+letter,
    	                    method: "GET",
    	                    params: {
    	                        "page":  $scope.currentPage
    	                    }
    	                }).success(function (data) {
    	                    $rootScope.page = data;
    	                    $rootScope.numberOfElements = data.numberOfElements;
    	                });
                    }
                    else{
                    	$http({
    	                    url: 'user/',
    	                    method: "GET",
    	                    params: {
    	                        "page":  $scope.currentPage
    	                    }
    	                }).success(function (data) {
    	                    $rootScope.page = data;
    	                    $rootScope.numberOfElements = data.numberOfElements;
    	                });
                    }
	                
            	}
            });
        });

})();
